package stdio.costa.imoca.utils;

import java.io.ByteArrayInputStream;
import java.io.InputStream;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang3.StringUtils;

import com.aspose.words.License;
import com.aspose.words.Node;

public class AsposeUtils {
	
	public static void loadLicense() {
		License license = new License();
		InputStream in = null;
		try {
			in = AsposeUtils.class.getClassLoader().getResourceAsStream("imoca.LIC");
			license.setLicense(new ByteArrayInputStream(Base64.decodeBase64(IOUtils.read(in))));
		} catch (Exception e) {
			throw new IllegalStateException("Failed to initialize Aspose license.");
		} finally {
			IOUtils.close(in);
		}
	}
	
	public static void replace(Node node, String target, String replacement) throws Exception {
		node.getRange().replace(target, StringUtils.trimToEmpty(replacement), true, false);
	}
}
